package calendarAlarmController

import (
	"GoLangTest/model/calendarAlarm"
	"GoLangTest/service/calendarAlarmService"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"io"
	"log"
	"net/http"
	"strconv"
	"time"
)

/* 新增 */
func Add(c *gin.Context) {
	information := calendarAlarm.CalendarAlarmInformation{}

	//这里模拟判断是否是生成本用户的提醒数据，一般来讲应该用token，然后由token获取当前用户信息来填入createUserId
	createUserIdStr := c.GetHeader("createUserId")
	if createUserIdStr == "" {
		c.JSON(http.StatusForbidden, gin.H{
			"msg": "操作不合法",
		})
		return
	}

	//从body中获取对应参数
	_ = c.ShouldBindJSON(&information)

	information.CreateUserId, _ = strconv.Atoi(createUserIdStr)
	bodyBytes, _ := io.ReadAll(c.Request.Body)
	fmt.Print(bodyBytes)
	result, err := calendarAlarmService.Add(information)
	c.JSON(http.StatusOK, gin.H{
		"data": result,
		"msg":  err,
	})
	return
}

/* 根据创建用户id查询所有记录 */
func GetListByCreateUserId(c *gin.Context) {
	//这里模拟判断是否是生成本用户的提醒数据，一般来讲应该用token，然后由token获取当前用户信息来填入createUserId
	createUserIdStr := c.GetHeader("createUserId")
	if createUserIdStr == "" {
		c.JSON(http.StatusForbidden, gin.H{
			"msg": "操作不合法",
		})
		return
	}
	createUserId, _ := strconv.Atoi(createUserIdStr)
	result := calendarAlarmService.GetListByCreateUserId(createUserId)
	c.JSON(http.StatusOK, gin.H{
		"data": result,
	})
	return
}

/* 更新 */
func Update(c *gin.Context) {
	information := calendarAlarm.CalendarAlarmInformation{}

	//这里模拟判断是否是生成本用户的提醒数据，一般来讲应该用token，然后由token获取当前用户信息来填入createUserId
	createUserIdStr := c.GetHeader("createUserId")
	//从body中获取对应参数
	_ = c.ShouldBindJSON(&information)

	if createUserIdStr == "" || information.Id == 0 {
		c.JSON(http.StatusForbidden, gin.H{
			"msg": "操作不合法",
		})
		return
	}

	information.CreateUserId, _ = strconv.Atoi(createUserIdStr)

	result, err := calendarAlarmService.Update(information)
	c.JSON(http.StatusOK, gin.H{
		"data": result,
		"msg":  err,
	})
	return
}

/* 根据id删除 (物理删除，正常来讲应该要考虑逻辑删除) */
func DeleteById(c *gin.Context) {
	information := calendarAlarm.CalendarAlarmInformation{}

	//这里模拟判断是否是生成本用户的提醒数据，一般来讲应该用token，然后由token获取当前用户信息来填入createUserId
	createUserIdStr := c.GetHeader("createUserId")
	//从body中获取对应参数
	_ = c.ShouldBindJSON(&information)

	if createUserIdStr == "" || information.Id == 0 {
		c.JSON(http.StatusForbidden, gin.H{
			"msg": "操作不合法",
		})
		return
	}

	information.CreateUserId, _ = strconv.Atoi(createUserIdStr)

	result, err := calendarAlarmService.DeleteById(information)
	c.JSON(http.StatusOK, gin.H{
		"data": result,
		"msg":  err,
	})
	return
}

/* webSocket提醒 */
func WebSocketAlarm(c *gin.Context) {
	//这里模拟判断是否是生成本用户的提醒数据，一般来讲应该用token，然后由token获取当前用户信息来填入createUserId
	createUserIdStr := c.GetHeader("createUserId")
	if createUserIdStr == "" {
		c.JSON(http.StatusForbidden, gin.H{
			"msg": "操作不合法",
		})
		return
	}

	upgrader := websocket.Upgrader{}
	// 升级HTTP连接为WebSocket连接
	ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		log.Println("Error upgrading to websocket:", err)
		return
	}
	defer ws.Close()

	// 启动一个goroutine来循环发送消息给客户端
	createUserId, _ := strconv.Atoi(createUserIdStr)
	for {
		go calendarAlarmService.AlarmUserByCreateUserId(createUserId, ws)
		time.Sleep(1 * time.Second) //实际操作这里应该用分钟刷新
	}
}
